﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Collections;
using System.Reflection;

namespace NMoq.Core
{
    public static class DelegateExtension
    {
        /// <summary>
        /// compare the two Delegate is equal, may have some bug   
        /// </summary>
        /// <param name="equalFrom"></param>
        /// <param name="equalTo"></param>
        /// <returns></returns>
        public static bool IsEquivalent(this Delegate implDelegate, MethodInfo methodInfo)
        {
            bool isEqual = true ;
            MethodInfo implMethodInfo = implDelegate.Method;
            isEqual &= implMethodInfo.ReturnType == methodInfo.ReturnType ;

            ParameterInfo[] implParamInfos = implMethodInfo.GetParameters();
            ParameterInfo[] toParamInfos = methodInfo.GetParameters();

            isEqual &= implParamInfos.Length == toParamInfos.Length;
            for (int i = 0; i < implParamInfos.Length; i++)
            {
                isEqual &= implParamInfos[i].IsIn == toParamInfos[i].IsIn;
                isEqual &= implParamInfos[i].IsOut  == toParamInfos[i].IsOut ;
                isEqual &= implParamInfos[i].ParameterType == toParamInfos[i].ParameterType;
                isEqual &= implParamInfos[i].Position == toParamInfos[i].Position;
            }
            return isEqual;
        }
    }
}
